package com.suyang.syt.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	private StringUtil() {

	}

	public static final String ERROR_MSG = "出错了!";
	public static final String ILLEGAL_MSG = "非法请求!";

	/** 字符串转输入流 */
	public static InputStream getInputStream(String str) {
		return new ByteArrayInputStream(str.getBytes());
	}

	/** 输入流转字符串 */
	public static String getStringByInputStream(InputStream inputStream) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuilder buffer = new StringBuilder();
		String line = "";
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		return buffer.toString();
	}

	public static String getSqlId(String ids) {
		if (EmptyUtil.isNotEmpty(ids)) {
			String[] idsArr = ids.split(",");
			StringBuilder sb = new StringBuilder();
			for (String id : idsArr) {
				sb.append("'").append(id).append("'").append(",");
			}
			return sb.toString().substring(0, sb.toString().length() - 1);
		}
		return null;
	}

	/** 验证是否是手机号 */
	public static boolean isMobileNo(String mobileNo) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher m = p.matcher(mobileNo);
		return m.matches();
	}

	/** 数字换成 #num# */
	public static String splitReplaceString(String str) {
		return str.replaceAll("([0-9]+)", "#$1#");
	}

	/** 手机号加密 */
	public static String replacePhone(String str) {
		if (EmptyUtil.isNotEmpty(str)) {
			return str.replaceAll("(\\d{3})\\d{6}(\\d{2})", "$1******$2");
		}
		return null;
	}

}
